﻿using UnityEngine;
using System.Collections;

public class CC : MonoBehaviour
{

    //public GameObject player;                               //摄像机的焦点
    //多点触控推拉
    private Vector2 tempPosition1;                          //触控点1的坐标
    private Vector2 tempPosition2;                          //触控点2的坐标
    private float tempDistance = 0;                         //这一帧触控点1和2的距离
    private float oldDistance = 0;                          //上一帧触控点1和2的距离
    private float deltaDistance = 0;                        //距离增量
    private Camera camera;                                  //Camera组件
    public float fieldofviewMin = 20;
    public float fieldofviewMax = 80;






    void Awake()
    {
        camera = GetComponent<Camera>();

    }


    void Update()
    {

		if (Input.touchCount >1)
        {

            //2个触控点有一个是移动中时
            if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                //获取2个触控点的坐标
                tempPosition1 = Input.GetTouch(0).position;
                tempPosition2 = Input.GetTouch(1).position;
                //记录这一帧两个触控点的距离
                tempDistance = Vector2.Distance(tempPosition1, tempPosition2);
                //记录的两个距离不相等时
                if (tempDistance != oldDistance && oldDistance != 0)
                {
                    //计算距离增量
                    deltaDistance = oldDistance - tempDistance;
                }
                //记录上一帧两个触控点的距离
                oldDistance = tempDistance;
            }
            //两点触控的参数初始化
            if (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Ended)
            {
                oldDistance = 0;
                deltaDistance = 0;
            }
        }

#if UNITY_EDITOR
        //编辑器模式下的距离增量
        deltaDistance = -Input.GetAxis("Mouse ScrollWheel") * 1000;
#endif
        //镜头推拉,限制fieldofview范围在25-75
        if (camera.fieldOfView + deltaDistance * 0.05f > fieldofviewMin && camera.fieldOfView + deltaDistance * 0.05f < fieldofviewMax)
        {
            camera.fieldOfView += deltaDistance * 0.05f;
        }
    }








}
